package com.hbq.codedemopersion.hbase;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;

/**
 * @ClassNAME HbaseDataSourceConfig
 * @Description TODO
 * @Author wangzb@cheche365.com
 * @Date 2022/10/19 4:01 PM
 * @Version 1.0
 */
@Slf4j
//@Configuration
public class HbaseDataSourceConfig {
    @Value("${hbase.zookeeper.quorum}")
    private String zookeeperQuorum;
    @Value("${hbase.zookeeper.client.port}")
    private String zookeeperClientPort;

//    @Bean("hBaseConnection")
    public Connection getHBaseConnection() {
        org.apache.hadoop.conf.Configuration config = HBaseConfiguration.create();
        config.set(HConstants.ZOOKEEPER_QUORUM, zookeeperQuorum);
        config.set(HConstants.ZOOKEEPER_CLIENT_PORT, zookeeperClientPort);
        config.setInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT, 10000);
        config.setInt(HConstants.HBASE_CLIENT_MAX_TOTAL_TASKS, 1000);
        config.setInt(HConstants.HBASE_CLIENT_IPC_POOL_SIZE, 128);
        config.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 3);
        Connection connection = null;
        try {
            connection = ConnectionFactory.createConnection(config);
        } catch (IOException e) {
            log.error("获取HBase连接失败!", e);
        }
        return connection;
    }

//    @Bean("hBaseAdmin")
    public Admin getAdmin() throws IOException {
        return getHBaseConnection().getAdmin();
    }
}
